﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data.Sql;
using System.Data.SqlClient;
using System .Net ;
using System.Data.EntityClient;


namespace 视频设备
{
    public partial class 设备主人界面 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindData();
            
        }

        void BindData()
        {
            using (Model1Container db=new Model1Container ())
            { 
             //将数据库绑定到GridView上
             GridView1.DataSource = db.观众账号集;
             GridView1.DataBind();
            }
        }

        //查询用户
        protected void Button1_Click(object sender, EventArgs e)
        {           
                using (Model1Container db = new Model1Container())
                {
                    观众账号 sh = new 观众账号();
                    var id = Convert .ToInt32 ( TextBox1.Text);
                    var name = TextBox2.Text;
                    List<观众账号> sheb = (from u in db.观众账号集 where u.用户名 == name || u .用户ID ==id  select u).ToList();
                    if (sheb.Count() > 0)
                    {
                        GridView1.DataSource = sheb;
                        GridView1.DataBind();
                    }
                    else
                    {
                        Response.Write("<script>alert('抱歉!没有您想要查到的用户!')</script>");
                    }
                }
        }          
        
        //删除用户
        protected void Button2_Click(object sender, EventArgs e)
        {
            if (TextBox2.Text == "")
            {
                Response.Write("<script>alert('请输想要删除的用户名!')</script>");
            }
            else
            {        
                var id = Convert .ToInt32 (TextBox1.Text);
                观众账号集.AuthResult delResult = 观众账号集.queryId(id);              
                switch (delResult)
                {
                    case 观众账号集 .AuthResult .Sucess :
                        Response.Write("<script>alert('确定要删除该用户名吗?')</script>");
                        delete();
                        break;
                    case 观众账号集 .AuthResult .DoesnotMatch :
                        Response.Write("<script>alert('抱歉!该用户不存在!')</script>");
                        break;
                }                  
            }
        }

        //修改用户权限
        protected void Button3_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "" && TextBox2.Text == "")
            {
                Response.Write("<script>alert('请输入要修改的用户名和ID号!')</script>");
            }
            else
            {                          
                var name = TextBox2.Text;
                观众账号集.AuthResult alterResult = 观众账号集.queryName(name);              
                switch (alterResult)
                {
                    case 观众账号集 .AuthResult .Sucess :
                        control();
                        break;
                    case 观众账号集 .AuthResult .DoesnotMatch :
                        Response.Write("<script>alert('抱歉!该用户不存在!')</script>");
                        break;
                }                
            }
        }
      
        //修改用户密码
        protected void Button5_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "" && TextBox2.Text == "")
            {
                Response.Write("<script>alert('请输入要修改的用户名和ID号!')</script>");
            }
            else
            {              
                var name = TextBox2.Text;
                var id =Convert .ToInt32 (TextBox1.Text);
                观众账号集.AuthResult queryResult = 观众账号集.queryNameId (name, id);              
                switch (queryResult)
                {
                    case 观众账号集 .AuthResult .Sucess :
                        altermima();
                        break;
                    case 观众账号集 .AuthResult .DoesnotMatch :
                        Response.Write("<script>alert('抱歉!用户名和ID不匹配!')</script>");
                        break;
                }              
            }
        }

        //修改用户权限
        void control()
        {
            Model1Container db = new Model1Container();
            观众账号 cmn = db.观众账号集.FirstOrDefault(cc => cc.用户名 == TextBox2.Text);
            cmn.权限 = DropDownList1.Text;
            观众账号 update = db.观众账号集.FirstOrDefault(cc => cc.用户名 == TextBox2.Text);
            db.SaveChanges();
            BindData();
        }


        //修改用户密码
        void altermima()
        {
            Model1Container db = new Model1Container();
            观众账号 cmn = db.观众账号集.FirstOrDefault(cc => cc.用户名 == TextBox2.Text);
            cmn.密码 = TextBox3.Text;
            观众账号 update = db.观众账号集.FirstOrDefault(cc => cc.用户名 == TextBox2.Text);
            db.SaveChanges();
            BindData();
        }


        //删除用户
        void delete()
        {
            using (Model1Container db = new Model1Container())
            {
                try
                {
                    观众账号 sh = new 观众账号();
                    sh.用户ID = Convert.ToInt32(TextBox1.Text);
                    sh.用户名 = TextBox2.Text;
                    sh.权限 = "";
                    db.观众账号集.Attach(sh);
                    db.ObjectStateManager.ChangeObjectState(sh, System.Data.EntityState.Deleted);
                    db.SaveChanges();
                    BindData();
                    Response.Write("删除成功!");
                }
                catch (Exception eo)
                {
                    Response.Write("<script>alert('抱歉!没有您想要删除的用户不存在!')</script>");
                }
            }
        }
    }
}